Modeling grouped or hierarchical data by allowing the intercept to vary for each group.
General Principles
To model the relationship between a dependent variable and an independent variable while allowing for different intercepts across groups or clusters, we can use a Varying Intercepts model. This approach is particularly useful when data are grouped (e.g., by subject, location, or time period) and we expect the baseline level of the outcome to vary across these groups.
The main idea of varying intercepts is to generate an intercept for each group, allowing each group to start at different levels. Thus, the intercept \alpha_k is defined uniquely for each of the K declared groups.
In the code below, the intercept alpha for each of the k declared groups shares two priors, a_bar and sigma, which are respectively modeled by a Normal and an Exponential distribution.
Example
Below is an example code snippet demonstrating Bayesian regression with varying intercepts using the BayesForge (BF) package. The data consists of a dependent variable representing individuals’ survival (surv) and an independent categorical variable (tank), which indicates the tank where the individual was born, with a total of 48 tanks. This example is based on McElreath (2018).
from BayesForge import bfimport jax.numpy as jnp# Setup device------------------------------------------------m = bf(platform='cpu')# Import Data & Data Manipulation ------------------------------------------------# Importfrom importlib.resources import filesdata_path = m.load.reedfrogs(only_path=True)m.data(data_path, sep=';') # Manipulatem.df["tank"] = jnp.arange(m.df.shape[0]) # Define model ------------------------------------------------def model(tank, surv, density): sigma = m.dist.exponential( 1, name ='sigma') a_bar = m.dist.normal( 0., 1.5, name ='a_bar') alpha = m.dist.normal( a_bar, sigma, shape= tank.shape, name ='alpha') p = alpha[tank] m.dist.binomial(total_count = density, logits = p, obs=surv)# Run sampler ------------------------------------------------m.fit(model, progress_bar=False) # Diagnostic ------------------------------------------------m.summary()
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
bf v 0.0.60 package loaded
jax.local_device_count(cpu) = 32
mean
sd
eti89_lb
eti89_ub
ess_bulk
ess_tail
r_hat
mcse_mean
mcse_sd
a_bar
1.34
0.25
0.95
1.75
4010.08
2589.08
1.0
0.00
0.00
alpha[0]
2.14
0.87
0.85
3.61
5208.21
2287.79
1.0
0.01
0.02
alpha[1]
3.08
1.11
1.51
5.02
5427.12
2767.81
1.0
0.02
0.02
alpha[2]
0.99
0.66
-0.04
2.11
5735.98
2889.39
1.0
0.01
0.01
alpha[3]
3.09
1.13
1.49
5.05
4119.56
1996.32
1.0
0.02
0.02
alpha[4]
2.12
0.87
0.85
3.63
5446.93
2798.27
1.0
0.01
0.02
alpha[5]
2.11
0.84
0.83
3.52
5498.20
3212.47
1.0
0.01
0.01
alpha[6]
3.07
1.12
1.47
5.02
5309.61
2556.65
1.0
0.02
0.02
alpha[7]
2.14
0.86
0.88
3.60
4835.32
2675.86
1.0
0.01
0.01
alpha[8]
-0.19
0.61
-1.16
0.76
5466.88
3035.74
1.0
0.01
0.01
alpha[9]
2.14
0.87
0.86
3.58
5300.20
2719.39
1.0
0.01
0.02
alpha[10]
1.01
0.67
-0.03
2.11
5677.76
2674.78
1.0
0.01
0.01
alpha[11]
0.59
0.65
-0.42
1.66
5227.47
2811.42
1.0
0.01
0.01
alpha[12]
1.01
0.67
0.00
2.12
6045.95
3052.42
1.0
0.01
0.01
alpha[13]
0.21
0.62
-0.80
1.18
5809.25
3295.08
1.0
0.01
0.01
alpha[14]
2.14
0.87
0.88
3.60
4312.11
2656.31
1.0
0.01
0.02
alpha[15]
2.13
0.89
0.85
3.67
4925.54
2973.91
1.0
0.01
0.02
alpha[16]
2.90
0.79
1.76
4.24
5069.12
2391.32
1.0
0.01
0.02
alpha[17]
2.39
0.65
1.44
3.50
5498.49
2686.59
1.0
0.01
0.01
alpha[18]
2.02
0.59
1.13
3.01
4044.71
2165.69
1.0
0.01
0.01
alpha[19]
3.67
0.99
2.25
5.41
3869.20
2551.48
1.0
0.02
0.02
alpha[20]
2.40
0.66
1.44
3.50
4654.71
2275.22
1.0
0.01
0.01
alpha[21]
2.39
0.66
1.40
3.54
5173.35
2736.55
1.0
0.01
0.01
alpha[22]
2.41
0.66
1.43
3.55
4646.96
2560.12
1.0
0.01
0.01
alpha[23]
1.71
0.53
0.92
2.59
5462.29
2630.25
1.0
0.01
0.01
alpha[24]
-0.99
0.44
-1.72
-0.29
5285.06
2951.43
1.0
0.01
0.01
alpha[25]
0.16
0.40
-0.50
0.82
4948.52
2958.90
1.0
0.01
0.01
alpha[26]
-1.43
0.50
-2.28
-0.67
5500.84
2657.37
1.0
0.01
0.01
alpha[27]
-0.47
0.40
-1.12
0.15
6068.78
3322.51
1.0
0.01
0.01
alpha[28]
0.16
0.39
-0.46
0.78
4919.46
3074.38
1.0
0.01
0.01
alpha[29]
1.45
0.49
0.69
2.27
5489.60
3100.29
1.0
0.01
0.01
alpha[30]
-0.63
0.41
-1.28
0.01
4301.34
3076.82
1.0
0.01
0.01
alpha[31]
-0.32
0.41
-0.97
0.33
5502.38
3020.06
1.0
0.01
0.01
alpha[32]
3.19
0.76
2.08
4.47
5070.22
2856.12
1.0
0.01
0.01
alpha[33]
2.71
0.65
1.76
3.83
5565.60
2687.46
1.0
0.01
0.01
alpha[34]
2.69
0.62
1.76
3.71
4537.37
2664.78
1.0
0.01
0.01
alpha[35]
2.06
0.50
1.30
2.90
5235.35
2722.72
1.0
0.01
0.01
alpha[36]
2.06
0.50
1.32
2.91
6128.81
2729.55
1.0
0.01
0.01
alpha[37]
3.89
0.97
2.52
5.54
4117.31
1970.45
1.0
0.02
0.02
alpha[38]
2.71
0.64
1.77
3.78
4944.30
2671.49
1.0
0.01
0.01
alpha[39]
2.36
0.57
1.51
3.33
5222.30
2600.98
1.0
0.01
0.01
alpha[40]
-1.80
0.47
-2.56
-1.10
5000.88
2652.28
1.0
0.01
0.01
alpha[41]
-0.56
0.36
-1.16
0.01
5791.42
2780.71
1.0
0.00
0.01
alpha[42]
-0.47
0.34
-1.02
0.09
5416.32
2832.71
1.0
0.00
0.01
alpha[43]
-0.34
0.35
-0.89
0.23
5865.32
2874.13
1.0
0.00
0.01
alpha[44]
0.58
0.34
0.04
1.14
5267.28
2754.55
1.0
0.00
0.01
alpha[45]
-0.58
0.36
-1.16
-0.01
5285.77
2713.84
1.0
0.01
0.01
alpha[46]
2.06
0.52
1.29
2.91
5496.29
2674.39
1.0
0.01
0.01
alpha[47]
0.00
0.32
-0.51
0.52
5817.47
3017.11
1.0
0.00
0.01
sigma
1.61
0.21
1.31
1.98
2496.50
2663.93
1.0
0.00
0.00
Code
from BayesForge import bfimport jax.numpy as jnp# Setup device------------------------------------------------m = bf(platform='cpu')# Import Data & Data Manipulation ------------------------------------------------# Importfrom importlib.resources import filesdata_path = m.load.reedfrogs(only_path=True)m.data(data_path, sep=';') # Manipulatem.df["tank"] = jnp.arange(m.df.shape[0]) # Define model ------------------------------------------------def model(tank, surv, density): alpha = m.effects.varying_intercept(N_groups=48,group_id=tank, group_name ='tank') m.dist.binomial(total_count = density, logits = alpha, obs=surv)# Run sampler ------------------------------------------------m.fit(model, progress_bar=False) # Diagnostic ------------------------------------------------m.summary()
jax.local_device_count(cpu) = 32
mean
sd
eti89_lb
eti89_ub
ess_bulk
ess_tail
r_hat
mcse_mean
mcse_sd
global_intercept_tank
1.36
0.26
0.94
1.78
4064.25
3075.09
1.0
0.00
0.00
intercept_tank[0]
2.14
0.85
0.86
3.61
5115.09
2985.82
1.0
0.01
0.01
intercept_tank[1]
3.06
1.12
1.46
5.00
4684.23
2653.30
1.0
0.02
0.02
intercept_tank[2]
1.02
0.67
0.00
2.11
5799.30
3063.85
1.0
0.01
0.01
intercept_tank[3]
3.08
1.11
1.46
4.97
4286.75
2450.62
1.0
0.02
0.02
intercept_tank[4]
2.16
0.90
0.87
3.72
5335.50
2662.82
1.0
0.01
0.02
intercept_tank[5]
2.15
0.89
0.84
3.66
4694.12
2274.02
1.0
0.01
0.02
intercept_tank[6]
3.09
1.15
1.43
5.06
4301.78
2135.88
1.0
0.02
0.02
intercept_tank[7]
2.15
0.86
0.86
3.63
5103.12
2772.26
1.0
0.01
0.02
intercept_tank[8]
-0.17
0.63
-1.16
0.82
5652.13
2581.50
1.0
0.01
0.01
intercept_tank[9]
2.13
0.87
0.83
3.63
5144.05
2919.17
1.0
0.01
0.02
intercept_tank[10]
1.00
0.68
-0.02
2.12
6161.49
2542.77
1.0
0.01
0.01
intercept_tank[11]
0.58
0.64
-0.41
1.61
5720.64
3183.37
1.0
0.01
0.01
intercept_tank[12]
1.01
0.67
0.00
2.09
6073.66
2811.46
1.0
0.01
0.01
intercept_tank[13]
0.19
0.61
-0.79
1.19
5630.95
3012.95
1.0
0.01
0.01
intercept_tank[14]
2.13
0.86
0.84
3.58
5505.94
2622.64
1.0
0.01
0.01
intercept_tank[15]
2.14
0.86
0.83
3.59
6145.71
2960.16
1.0
0.01
0.01
intercept_tank[16]
2.92
0.81
1.75
4.31
4678.60
2632.66
1.0
0.01
0.02
intercept_tank[17]
2.41
0.67
1.42
3.53
4928.00
3056.23
1.0
0.01
0.01
intercept_tank[18]
2.00
0.58
1.16
2.96
5927.19
2844.18
1.0
0.01
0.01
intercept_tank[19]
3.67
1.01
2.24
5.41
4579.11
2572.12
1.0
0.02
0.02
intercept_tank[20]
2.40
0.65
1.43
3.50
5477.44
2582.43
1.0
0.01
0.01
intercept_tank[21]
2.40
0.65
1.44
3.52
4925.81
2621.66
1.0
0.01
0.01
intercept_tank[22]
2.41
0.67
1.41
3.54
4839.13
2580.81
1.0
0.01
0.01
intercept_tank[23]
1.71
0.54
0.90
2.58
4895.73
2940.08
1.0
0.01
0.01
intercept_tank[24]
-1.00
0.46
-1.77
-0.30
5125.53
2847.18
1.0
0.01
0.01
intercept_tank[25]
0.16
0.42
-0.50
0.84
6002.24
2965.77
1.0
0.01
0.01
intercept_tank[26]
-1.43
0.48
-2.24
-0.69
5445.70
2980.02
1.0
0.01
0.01
intercept_tank[27]
-0.47
0.41
-1.14
0.18
5127.80
3223.23
1.0
0.01
0.01
intercept_tank[28]
0.16
0.41
-0.47
0.82
5857.59
2970.07
1.0
0.01
0.01
intercept_tank[29]
1.45
0.49
0.70
2.24
6039.46
3064.00
1.0
0.01
0.01
intercept_tank[30]
-0.63
0.42
-1.32
0.01
4969.33
2865.25
1.0
0.01
0.01
intercept_tank[31]
-0.31
0.41
-0.95
0.33
6211.75
3130.70
1.0
0.01
0.01
intercept_tank[32]
3.19
0.77
2.09
4.51
4337.65
2677.57
1.0
0.01
0.01
intercept_tank[33]
2.71
0.63
1.76
3.82
4505.59
2591.38
1.0
0.01
0.01
intercept_tank[34]
2.72
0.66
1.77
3.85
4745.60
2561.29
1.0
0.01
0.01
intercept_tank[35]
2.06
0.51
1.29
2.91
4887.78
2945.75
1.0
0.01
0.01
intercept_tank[36]
2.06
0.51
1.28
2.93
5542.07
2586.96
1.0
0.01
0.01
intercept_tank[37]
3.92
0.97
2.55
5.56
4693.84
2728.10
1.0
0.02
0.02
intercept_tank[38]
2.73
0.66
1.78
3.87
4343.67
2473.52
1.0
0.01
0.01
intercept_tank[39]
2.34
0.57
1.50
3.33
5543.44
2659.39
1.0
0.01
0.01
intercept_tank[40]
-1.81
0.47
-2.60
-1.10
4692.33
2721.63
1.0
0.01
0.01
intercept_tank[41]
-0.57
0.36
-1.14
-0.02
5839.89
2950.68
1.0
0.00
0.01
intercept_tank[42]
-0.45
0.34
-1.00
0.09
4960.79
3178.95
1.0
0.00
0.01
intercept_tank[43]
-0.33
0.33
-0.87
0.21
4683.65
3046.84
1.0
0.00
0.01
intercept_tank[44]
0.58
0.35
0.02
1.13
5172.21
3004.93
1.0
0.00
0.01
intercept_tank[45]
-0.58
0.35
-1.15
-0.03
5909.60
2591.86
1.0
0.00
0.01
intercept_tank[46]
2.06
0.51
1.29
2.91
5379.67
3285.35
1.0
0.01
0.01
intercept_tank[47]
0.01
0.33
-0.52
0.53
5668.13
2697.20
1.0
0.00
0.01
sd_tank
1.62
0.22
1.31
1.98
2400.37
2536.86
1.0
0.00
0.00
library(BayesForge)# setup platform------------------------------------------------m=importBF(platform='cpu')# Import data ------------------------------------------------m$data(m$load$reedfrogs(only_path=T), sep=';')m$df$tank =as.integer(0:(nrow(m$df)-1)) # Manipulatem$data_to_model(list('tank', 'surv', 'density')) # Manipulatem$data_on_model$tank = m$data_on_model$tank$astype(jnp$int32) # Manipulatem$data_on_model$surv = m$data_on_model$surv$astype(jnp$int32) # Manipulatem$data_on_model$density = m$data_on_model$density$astype(jnp$int64) # Manipulate# Define model ------------------------------------------------model <-function(tank, surv, density){# Parameter prior distributions sigma = m$dist$exponential( 1, name ='sigma') a_bar = m$dist$normal(0, 1.5, name ='a_bar') alpha = m$dist$normal(a_bar, sigma, name ='alpha', shape = reticulate::tuple(48L)) p = alpha$`__getitem__`(tank)# Likelihood m$dist$binomial(total_count = density, logits = p, obs = surv, name ='surv')} # Run MCMC ------------------------------------------------m$fit(model) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m$summary() # Get posterior distribution
usingPyBayesForge# Setup device------------------------------------------------m =importBF(platform="cpu")# Import Data & Data Manipulation ------------------------------------------------# Importdata_path = m.load.reedfrogs(only_path =true)m.data(data_path, sep=';')m.df["tank"] = jnp.arange(m.df.shape[0]) # Define model ------------------------------------------------@BFfunctionmodel(tank, surv, density) alpha = m.effects.varying_intercept(N_groups=48,group_id=tank, group_name ="tank") m.dist.binomial(total_count = density, logits = alpha, obs=surv)end# Run mcmc ------------------------------------------------m.fit(model) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m.summary() # Get posterior distributions
Mathematical Details
We model the relationship between the independent variable X and the outcome variable Y while accounting for varying intercepts \alpha for each group where k(i) give us group belonging for observation i, using the following equation:
\alpha_{[k(i)]} is the varying intercept corresponding to the group k of observation i%.
\beta is the regression coefficient.
\sigma is a standard deviation parameter, which here has an Exponential prior that constrains it to be positive.
\bar{\alpha} is the overall mean intercept.
\varsigma is the variance of the intercepts across groups.
Model validation tests
Comparison with Stan: 11.Varying intercepts.py fits the same model with BayesForge and Stan and compares the posterior distributions (KL divergence).
Data generative model and inference model pipeline: 11.Varying intercepts.py simulates data from the model with known parameters (data generative model), fits the inference model to each simulated dataset and checks that the parameters are recovered.